package demo04;

import java.util.Scanner;

public class StudentFactory {
    public static Student studentFactory() {
        Student temp = new Student();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生姓名：");
        temp.setName(sc.nextLine());
        System.out.println("请输入语文成绩：");
        temp.setChineseGrade(getNumByScannerBetween1To100());
        System.out.println("请输入数学成绩：");
        temp.setMathGrade(getNumByScannerBetween1To100());
        System.out.println("请输入英语成绩：");
        temp.setEnglishGrade(getNumByScannerBetween1To100());
        return temp;
    }

    /**
     * 键盘录入一个1到100的数字并返回.
     *
     * @return 键盘录入一个1到100的数字并返回.
     */
    public static int getNumByScannerBetween1To100() {
        Scanner sc = new Scanner(System.in);
        int grade = 0;
        try {
            while (true) {
                grade = Integer.parseInt(sc.nextLine());
                if (grade > 100 || grade < 0)
                    System.out.println("输入不合法重新输入！");
                else
                    break;
            }
        } catch (Exception e) {
            System.out.println("程序出错：" + e.getMessage());
        }
        return grade;
    }
}
